//
//  ViewController.m
//  onebuycheckstand
//
//  Created by xu yao on 2021/3/1.
//

#import "ViewController.h"
#import <WebKit/WebKit.h>
#import "AppConfig.h"
#import <UPaySDK/UPaySDK.h>
//#import "HCToast.h"

//#import <UPaySDK/UPaySDK.h>
@interface ViewController () <WKNavigationDelegate, WKScriptMessageHandler, WKUIDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) WKWebView* wkWebview;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    self.navigationItem.title = @"uppv2支付下单";
    // 初始化webview
    [self initWebView];
    // 注册js方法
    [self initJsMethod];
}
//#define WEBKIT_ACTION_GET_DEVICE_INFO @"getDeviceInfo"
- (void)initJsMethod {
    [self.wkWebview.configuration.userContentController addScriptMessageHandler:self name:@"arouseCashdesk"];
    [self.wkWebview.configuration.userContentController addScriptMessageHandler:self name:@"getDeviceInfo"];
}

- (void)initWebView {
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    [configuration setValue:@YES forKey:@"_allowUniversalAccessFromFileURLs"];//允许跨域
    [configuration.preferences setValue:@"TRUE" forKey:@"allowFileAccessFromFileURLs"];//允许加载沙盒路径
    configuration.preferences = [WKPreferences new];
    configuration.preferences.minimumFontSize = 10;
    configuration.preferences.javaScriptEnabled = YES;
    configuration.preferences.javaScriptCanOpenWindowsAutomatically = YES;
    
    WKUserContentController *userController = [WKUserContentController new];
    NSString *js = @" $('meta[name=description]').remove(); $('head').append( '' );";
    WKUserScript *script = [[WKUserScript alloc] initWithSource:js injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:NO];
    NSMutableString *javascript = [NSMutableString string];
    [javascript appendString:@"document.documentElement.style.webkitTouchCallout='none';"];//禁止长按
    [javascript appendString:@"document.documentElement.style.webkitUserSelect='none';"];//禁止选择

    WKUserScript *noneSelectScript = [[WKUserScript alloc] initWithSource:javascript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    [userController addUserScript:script];
    [userController addUserScript:noneSelectScript];
    configuration.userContentController = userController;
    [configuration.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"];
    if (@available(iOS 10.0, *)) {
         [configuration setValue:@YES forKey:@"allowUniversalAccessFromFileURLs"];
    }
    WKWebView *webview = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height) configuration:configuration];
    webview.navigationDelegate = self;
    webview.UIDelegate = self;
    webview.scrollView.scrollEnabled = NO;//@"http://10.10.11.107:21819/"@"http://10.10.15.158:8081/"
    [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://10.10.11.107:21819/"]]];
    [self.view addSubview:webview];
    webview.backgroundColor = [UIColor whiteColor];
    self.wkWebview = webview;
    [webview evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
        NSString *userAgent = result;
        NSString *newUserAgent = [userAgent stringByAppendingFormat:@"/%@", CUSTOM_USER_AGENT];
        [self.wkWebview setCustomUserAgent:newUserAgent];
    }];
}

#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
//    NSURL *URL = navigationAction.request.URL;
//    NSString *scheme = [URL scheme];
//    if ([URL.absoluteString containsString:@"https://uppweb.gogbuy.com"]) {
//        [[UIApplication sharedApplication] openURL:URL];
//        NSLog(@"%@",URL.absoluteString);
//        decisionHandler(WKNavigationActionPolicyCancel);
//        return;
//    }
    decisionHandler(WKNavigationActionPolicyAllow);
}

#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    if ([message.name isEqualToString:@"arouseCashdesk"]) {
        /** 调用收银平台SDK */
        if (message.body[@"param"]) {
            [UPaySDK payInfoPageWithCashStageSessionId:message.body[@"param"][@"cashdeskUrl"] onPayResult:^(USDKPlatformType platformType, NSInteger resultCode, id error) {
                NSLog(@"%@", error);
            }];
        }
    }
}


@end
